package cn.ante.org.controller;

import cn.ante.basic.annotation.PreAuthorize;
import cn.ante.basic.utils.AjaxResult;
import cn.ante.basic.utils.LoginContext;
import cn.ante.basic.utils.PageList;
import cn.ante.org.domain.Department;
import cn.ante.org.query.DepartmentQuery;
import cn.ante.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Api(value = "部门的api",description="部门相关的crud功能")
@RestController
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService service;

    /*
    * @Title: loadAll
    * @Description: 分页查询部门所有
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/6/24 11:49
    * @Parameters: [departmentQuery]
    * @Return cn.ante.basic.utils.PageList<cn.ante.org.domain.Department>
    */
    @PostMapping
    @ApiOperation(value = "分页查询部门列表")
    @PreAuthorize(name="分页查询部门列表", sn="department:list")
    public PageList<Department> findAll(@RequestBody DepartmentQuery departmentQuery, HttpServletRequest req){

        return service.findAll(departmentQuery);

    }

    /*
    * @Title: loadById
    * @Description: 根据id查询部门信息
    * @Author: Nai
    * @Version: 1.0
    * @Date:  2022/6/24 11:51
    * @Parameters: [id]
    * @Return cn.ante.org.domain.Department
    */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过id查询部门信息")
    @PreAuthorize(name="通过id查询部门信息", sn="department:get")
    public Department loadById(@ApiParam(name="主键",required = true) @PathVariable("id") Long id){
        return service.loadById(id);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除部门")
    @PreAuthorize(name ="根据id删除部门",sn="department:delete")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            service.remove(id);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    @PutMapping
    @ApiOperation(value = "保存修改部门")
    @PreAuthorize(name ="保存/修改部门",sn="department:addOrUpt")
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if (department.getId() == null){
                service.add(department);
            }else{
                service.update(department);
            }
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！！");
        }
    }


    @PatchMapping
    @ApiOperation(value = "批量删除部门")
    @PreAuthorize(name ="批量删除部门",sn="department:batchDel")
    public AjaxResult batchDel( @RequestBody Long[] ids){
        try {
            service.batchDel(ids);
            return AjaxResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    @GetMapping("/tree")
    @ApiOperation(value = "获取部门级联树")
    public List<Department> getDepartmentTree(){
//        tree.forEach(System.out::println);
        return service.tree();

    }

    @GetMapping
    @ApiOperation(value = "部门列表")
    public List<Department> loadAll(){
        return service.loadAll();

    }
}
